package com.wjb.web.backend;

import com.wjb.model.EbComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/Comment")
public class CommentController {

        @Autowired
        private com.wjb.service.CommentService CommentService;
        /**
         * 将数据放到 model中，将页面放到view中
         * spring mvc---将model渲染到view中
         *
         * list ---分页请求数据
         */

        @RequestMapping("/{id}")
        public String delete(@PathVariable("id") Integer id){
            int result=CommentService.delete(id);
            if(result==0){

            }
            return "manage/manage-result";
        }

    @RequestMapping("/edit")
    public String edit(Model model, Integer ecId) {
//调用service层查询所有信息
        EbComment ebComment = CommentService.selectById(ecId);
        //model传递与jsp指定
        model.addAttribute("ebComment", ebComment);
        return "manage/Comment-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbComment ebComment){
        //更新信息
        int result = CommentService.updateById(ebComment);
        //跳转刷新页面
        if(result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
        @RequestMapping("/list")
        public String list(Model model,@RequestParam(value = "page",defaultValue = "1") int psize,
                           @RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
            System.out.println("start service.....");
            //1.请求service层获取新闻的分页数据
            List<EbComment> CommentList = CommentService.listByPage(psize,pageSize);

            //d调用service层的新闻表总页数
            int totalPage = CommentService.getMaxPage(pageSize);
            //2.传递model(数据)和view视图(jsp)
            model.addAttribute( "CommentList",CommentList);
            model.addAttribute("totalPage",totalPage);
            model.addAttribute("curPage",psize);

            return "manage/comment";//视图  通过manage/news.jsp进行展示数据

        }



    }
